// ignore_for_file: constant_identifier_names
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/exam/dto/assessment.response.dart';
import 'controllers/ExamPageController.dart';

///
// Part 试卷章节， 最高级别的 章节
///
// ignore: must_be_immutable
class ExamQuestionIndexPage extends GetView<ExamPageController> {
  ExamQuestionIndexPage({Key? key}) : super(key: key);
  var textStyleTitle = TextStyle(fontWeight: FontWeight.bold, color: Get.isDarkMode ? Colors.white : Colors.black, fontSize: 24);
  var textStylePartTitle = TextStyle(fontWeight: FontWeight.bold, color: Get.isDarkMode ? Colors.white : Colors.black, fontSize: 20);
  var textStyleSectionTitle = const TextStyle(fontWeight: FontWeight.bold, color: Colors.blue, fontSize: 18);
  var textStyleQuestionTitle = TextStyle(fontWeight: FontWeight.normal, color: Get.isDarkMode ? Colors.white : Colors.black87, fontSize: 16);
  var textRedStyleQuestionTitle = TextStyle(fontWeight: FontWeight.normal, color: Get.isDarkMode ? Colors.white60 : Colors.red, fontSize: 16);

  @override
  Widget build(BuildContext context) {
    var indexContentBorder = const BoxDecoration(
      border: Border.fromBorderSide(BorderSide(width: 1, color: Colors.black12, style: BorderStyle.solid)),
    );
    var indexContentShape = const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(5)));
    var actions = <Widget>[
      IconButton(
        icon: const Icon(Icons.cancel),
        color: Colors.red,
        iconSize: 40,
        onPressed: () {
          Get.back();
        },
      ),
    ];
    _getAnswerStatus();
    return AlertDialog(
      title: Text('QUESTION INDEX'.tr),
      titleTextStyle: textStyleTitle,
      backgroundColor: Get.isDarkMode ? Colors.black : Colors.white,
      shape: indexContentShape,
      content: Padding(
          padding: const EdgeInsets.all(0.0),
          child: Container(
              decoration: indexContentBorder, padding: const EdgeInsets.all(8.0), width: 420, height: 420, child: _buildQuestionIndexContent())),
      actions: actions,
      // contentPadding: const EdgeInsets.all(0.0),
      buttonPadding: const EdgeInsets.all(0.0),
      actionsPadding: const EdgeInsets.all(0.0),
    );
  }

  void _getAnswerStatus() {
    var result = controller.assessmentResult;
    var partIndexList = controller.partIndexList;
    var partDataList = result!.list;
    for (var pIndex = 0; pIndex < partDataList!.length; pIndex++) {
      var partData = partDataList[pIndex];
      var partIndex = partIndexList[pIndex];
      var sectionDataList = partData.questions;
      for (var sIndex = 0; sIndex < sectionDataList!.length; sIndex++) {
        var sectionData = sectionDataList[sIndex];
        var sectionIndex = partIndex.sectionIndexList![sIndex];
        var questionDataList = sectionData.question;
        for (var qIndex = 0; qIndex < questionDataList!.length; qIndex++) {
          var item = questionDataList[qIndex];
          var questionIndex = sectionIndex.questionIndexList![qIndex];
          bool isHaveAnswer = false;
          if (item.answer != null && item.answer!.length > 0 && item.answer![0].isNotEmpty) {
            isHaveAnswer = true;
          }
          questionIndex.isHaveAnswer = isHaveAnswer;
        }
      }
    }
  }

  Widget _buildQuestionIndexContent() {
    var partIndexList = controller.partIndexList;
    List<Widget> partWidget = _getPartWidgetList(partIndexList);

    Widget questionIndexContent = Scrollbar(
        thumbVisibility: true,
        child: SingleChildScrollView(
            primary: true,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: partWidget,
            )));
    return questionIndexContent;
  }

  List<Widget> _getPartWidgetList(List<UiPartIndex> partIndexList) {
    List<Widget> partWidget = partIndexList.map((ePart) {
      Container sectionListContainer = _getSectionListContainer(ePart);
      var partTitle = Text(ePart.name, style: textStylePartTitle);
      var part = Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [Visibility(visible: partIndexList.length > 1, child: partTitle), sectionListContainer],
      );
      return part;
    }).toList();
    return partWidget;
  }

  Container _getSectionListContainer(UiPartIndex ePart) {
    var sectionList = ePart.sectionIndexList?.map((eSection) {
      Wrap questionListContainer = _getQuestionListContainer(eSection);
      var sectionTitle = Text(eSection.name, style: textStyleSectionTitle);
      var section = Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [sectionTitle, questionListContainer],
      );
      return section;
    }).toList();

    var sectionListContainer = Container(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [...?sectionList],
        ));
    return sectionListContainer;
  }

  Wrap _getQuestionListContainer(UiSectionIndex eSection) {
    List<Widget>? questionList = eSection.questionIndexList
        ?.map((e) {
          return Container(
              padding: const EdgeInsets.all(5),
              child: GestureDetector(
                  onTap: () {
                    Get.back();
                    onQuestionItemClick(e);
                  },
                  child: Text(e.name, style: e.isHaveAnswer ? textRedStyleQuestionTitle : textStyleQuestionTitle)));
        })
        .cast<Widget>()
        .toList();
    var questionListContainer = Wrap(
      spacing: 2, //主轴上子控件的间距
      runSpacing: 5, //交叉轴上子控件之间的间距
      children: questionList!, //要显示的子控件集合
    );
    return questionListContainer;
  }

  void onQuestionItemClick(UiQuestionIndex e) {
    Log.debug('onQuestionItemClick ${e.partIndex} ${e.sectionIndex} ${e.questionIndex} ${e.questionStepIndex}');
    controller.gotoPage(e.questionStepIndex ?? 0);
  }
}

class UiPartIndex {
  UiPartIndex(this.id, this.name, {this.sectionIndexList});

  String id;
  String name;
  List<UiSectionIndex>? sectionIndexList = [];
}

class UiSectionIndex {
  UiSectionIndex(this.id, this.name, {this.questionIndexList});

  String id;
  String name;
  List<UiQuestionIndex>? questionIndexList = [];
}

class UiQuestionIndex {
  UiQuestionIndex(this.id, this.name, {this.partIndex, this.sectionIndex, this.questionIndex, this.questionStepIndex, this.isHaveAnswer = false});

  String id;
  String name;

  int? questionStepIndex;
  int? partIndex;
  int? sectionIndex;
  int? questionIndex;
  bool isHaveAnswer;
}

/*
* 问题关卡 */
class UiQuestionStep {
  UiQuestionStep(this.index, this.name, this.stepType, {this.partIndex, this.sectionIndex, this.questionIndex});

  int index;
  String name;
  String stepType;

  int? partIndex;
  int? sectionIndex;
  int? questionIndex;

  static const String STEPTYPE_PARTPROMPT = "partprompt";
  static const String STEPTYPE_QUESTION = "question";
}

class UiQuestionStepTopic {
  UiQuestionStepTopic({this.head});

  QuestionContent? head;
}

class UiQuestionStepBody {
  UiQuestionStepBody({this.item});

  QuestionItem? item;
}
